package com.autumn.util;

import java.math.BigDecimal;

/**
 * 数字工具
 * 
 * @author 老码农 2018-03-27 22:11:49
 */
public class NumberUtils {

	/**
	 * 整数0
	 */
	public static final int NUM_INT_ZERO = 0;

	/**
	 * 整数1
	 */
	public static final int NUM_INT_1 = 1;
	/**
	 * 整数2
	 */
	public static final int NUM_INT_2 = 2;
	/**
	 * 整数3
	 */
	public static final int NUM_INT_3 = 3;
	/**
	 * 整数4
	 */
	public static final int NUM_INT_4 = 4;
	/**
	 * 整数5
	 */
	public static final int NUM_INT_5 = 5;
	/**
	 * 整数6
	 */
	public static final int NUM_INT_6 = 6;
	/**
	 * 整数7
	 */
	public static final int NUM_INT_7 = 7;
	/**
	 * 整数8
	 */
	public static final int NUM_INT_8 = 8;
	/**
	 * 整数9
	 */
	public static final int NUM_INT_9 = 9;
	/**
	 * 整数10
	 */
	public static final int NUM_INT_10 = 10;

	/**
	 * 整数16
	 */
	public static final int NUM_INT_16 = 16;

	/**
	 * Map\List 默认大小
	 */
	public static final int MAP_LIST_DEFAULT_SIZE = 16;

	/**
	 * 整数100
	 */
	public static final int NUM_INT_100 = 100;

	private static final BigDecimal RMB_MAX_VALUE = new BigDecimal("9999999999999999.99");
	private static final String RMB_CN_FULL = "整";
	private static final String RMB_CN_FULL_BRANCH = "零分";
	private static final String RMB_CN_NEGATIVE = "负";
	private static final String RMB_CN_ZEOR_FULL = "零元整";
	private static final String[] RMB_CN_UPPER_NUMBER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
	private static final String[] RMB_CN_UPPER_MONETRAY_UNIT = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿",
			"拾", "佰", "仟", "兆" /* 万亿 */, "拾", "佰", "仟" };
	private static final int RMB_MONEY_DIGITS = 2;

	/**
	 * 人民币大写转换
	 * 
	 * @param numberOfMoney 值
	 * @return
	 */
	public static String rmbUpperConvert(BigDecimal numberOfMoney) {
		if (numberOfMoney == null) {
			return RMB_CN_ZEOR_FULL;
		}
		numberOfMoney = BigDecimalUtils.roundUp(numberOfMoney, NUM_INT_4);
		if (BigDecimalUtils.gt(numberOfMoney, RMB_MAX_VALUE)) {
			ExceptionUtils.throwArgumentOverflowException("numberOfMoney", "超过最大转换值 " + RMB_MAX_VALUE.toString());
		}
		int signum = numberOfMoney.signum();
		if (BigDecimalUtils.eqZero(numberOfMoney)) {
			return RMB_CN_ZEOR_FULL;
		}
		StringBuffer sb = new StringBuffer();
		long number = numberOfMoney.movePointRight(RMB_MONEY_DIGITS).setScale(0, BigDecimal.ROUND_HALF_UP).abs()
				.longValue();
		int num_10 = NumberUtils.NUM_INT_10;
		int num_100 = NumberUtils.NUM_INT_100;
		int zero = NumberUtils.NUM_INT_ZERO;
		long scale = number % num_100;
		int numUnit = 0;
		int numIndex = 0;
		boolean getZero = false;
		if (!(scale > zero)) {
			numIndex = 2;
			number = number / num_100;
			getZero = true;
		}
		if ((scale > zero) && (!(scale % num_10 > zero))) {
			numIndex = 1;
			number = number / num_10;
			getZero = true;
		}
		int zeroSize = 0;
		while (true) {
			if (number <= zero) {
				break;
			}
			numUnit = (int) (number % num_10);
			if (numUnit > zero) {
				if ((numIndex == 9) && (zeroSize >= 3)) {
					sb.insert(0, RMB_CN_UPPER_MONETRAY_UNIT[6]);
				}
				if ((numIndex == 13) && (zeroSize >= 3)) {
					sb.insert(0, RMB_CN_UPPER_MONETRAY_UNIT[10]);
				}
				sb.insert(0, RMB_CN_UPPER_MONETRAY_UNIT[numIndex]);
				sb.insert(0, RMB_CN_UPPER_NUMBER[numUnit]);
				getZero = false;
				zeroSize = 0;
			} else {
				++zeroSize;
				if (!(getZero)) {
					sb.insert(0, RMB_CN_UPPER_NUMBER[numUnit]);
				}
				if (numIndex == 2) {
					if (number > zero) {
						sb.insert(0, RMB_CN_UPPER_MONETRAY_UNIT[numIndex]);
					}
				} else if (((numIndex - 2) % 4 == zero) && (number % 1000 > zero)) {
					sb.insert(0, RMB_CN_UPPER_MONETRAY_UNIT[numIndex]);
				}
				getZero = true;
			}
			number = number / num_10;
			++numIndex;
		}
		if (signum == -1) {
			sb.insert(0, RMB_CN_NEGATIVE);
		}
		if (scale <= zero) {
			sb.append(RMB_CN_FULL);
		} else {
			if (scale % num_10 == zero) {
				sb.append(RMB_CN_FULL_BRANCH);
			}
		}
		return sb.toString();
	}

}
